//
//  SEGMineCarListViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/5.
//

#import "SEGMineCarListViewModel.h"

@implementation SEGMineCarListViewModel


#pragma mark - life cycle


#pragma mark - init


#pragma mark - send request
- (RACSignal *)sendMineRentalListRequest {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        [self.requestManager requestMineCarRentalListWithFinished:^(id result, NSError *error) {
            if (!error) {
                if ([result isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *dict = result;
                    if (dict[@"data"]&&[dict[@"data"] isKindOfClass:[SEGMineCarRentallistDataModel class]]) {
                        SEGMineCarRentallistDataModel *model = dict[@"data"];
                        self.mineRentalArray = [model.myParkingRentalList mutableCopy];
                    }
                }
            }else {
                [subscriber sendError:error];
            }
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    return signal;
    
}

- (RACSignal * )sendDeleteRequestWithRentalId:(NSString *)rentalId {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        [self.requestManager requestDeleteMineCarWithRentalId:rentalId finished:^(id result, NSError *error) {
            if (!error) {
                //成功之后 删除那行数据
                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rentalId == %@", rentalId];
                NSArray <SEGCarRentalListItemModel *> *deleteArr = [self.mineRentalArray filteredArrayUsingPredicate:predicate];
                if (deleteArr.count > 0 &&[self.mineRentalArray containsObject:deleteArr.firstObject]) {
                    NSInteger index = [self.mineRentalArray indexOfObject:deleteArr.firstObject];

                    [self.mineRentalArray removeObjectAtIndex:index];
                    [subscriber sendNext:@(index)];
                }
            }else {
                [subscriber sendError:error];
            }
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
    return signal;
   
    
    
}
//下架
- (RACSignal *)sendoffLineRequestWithRentalId:(NSString *)rentalId {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        [self.requestManager requestOffLineMineCarWithRentalId:rentalId releaseType:@"5" finished:^(id result, NSError *error) {
            if (!error) {
                //成功之后 刷新数据
                [self sendMineRentalListRequest];
            }else {
                [subscriber sendError:error];
            }
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
    return signal;
    
}

//擦亮
- (RACSignal *)sendShineRequestWithRentalId:(NSString *)rentalId {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        [self.requestManager requestShineMineCarWithRentalId:rentalId  finished:^(id result, NSError *error) {
            if (!error) {
                //成功之后 刷新数据
                [subscriber sendNext:nil];
            }else {
                [subscriber sendError:error];
            }
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
    return signal;
    
}

#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response

#pragma mark - private methods

#pragma mark - setter and getter
- (SEGHouseRentalAndSaleRequestManager *)requestManager {
    if (!_requestManager) {
        _requestManager = [[SEGHouseRentalAndSaleRequestManager alloc] init];
    }
    return _requestManager;
}

@end
